package me.send.produce.client.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.send.business.base.ErrorEntity;
import me.send.business.base.SendVariable;
import me.send.business.base.oss.OSSObjectSample;
import me.send.business.service.bean.ReturnJsonBean;
import me.send.business.service.pojo.User;
import me.send.business.service.service.OrderBrokerService;
import me.send.business.service.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping(value = "user")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	OrderBrokerService orderBrokerService;

	
	@RequestMapping("refresh")
	@ResponseBody
	public Map<String, Object> refresh(String devicenumber, int userid){
		return userService.refresh(devicenumber, userid);
	}
	
	// 更新用户资料 2015年6月3日17:40:31
	@RequestMapping("update/header")
	@ResponseBody
	public Map<String, Object> update_header(
	// @RequestParam(value = "headerpath", required = false) MultipartFile
			// multipartFile,
			HttpServletRequest request, int userid, String devicenumber)
			throws Exception {
		// 转型为MultipartHttpRequest(重点的所在)
		MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
		// 获得第1张图片（根据前台的name名称得到上传的文件）
		MultipartFile multipartFile = multipartHttpServletRequest
				.getFile("headerpath");
		String originalFilename = multipartFile.getOriginalFilename();
		String format = originalFilename.substring(originalFilename
				.lastIndexOf("."));
		String key = userid + "_"
				+ System.currentTimeMillis() + format;
		String resultPath = OSSObjectSample.OSSPutObject(key, multipartFile, SendVariable.PRODUCE_CLIENT+"/user");

		// 将headerpath 放在数据
		return userService.update_header(devicenumber,resultPath, userid);
	}

	// 修改用户信息 2015年6月4日09:42:01
	@RequestMapping("update/user")
	@ResponseBody
	public ErrorEntity update_user(String devicenumber, User user) {
		return userService.update_user(devicenumber, user);
	}

	// 修改密码 用户 2015年6月1日16:00:51
	@RequestMapping(value = "updatePassword")
	@ResponseBody
	public ErrorEntity updatePassword(String telephone, String devicenumber,
			String code, String password) {
		return userService.updatePassword(telephone, devicenumber, code,
				password);
	}

	// 发送验证码
	@RequestMapping(value = "sendCode")
	@ResponseBody
	public Map<String, Object> sendCode(String telephone, String devicenumber) {
		return userService.sendCode(telephone, devicenumber);
	}

	// 验证 手机号码是否注册过
	@RequestMapping(value = "verification")
	@ResponseBody
	public ErrorEntity verification(String telephone, String devicenumber) {
		return userService.verification(telephone, devicenumber);
	}

	// 用户登录
	@RequestMapping(value = "login")
	@ResponseBody
	public Map<String, Object> login(String devicenumber, User user) {
		return userService.login(user, devicenumber);
	}

	// 用户注册
	@RequestMapping(value = "register")
	@ResponseBody
	public ErrorEntity register(String telephone, String devicenumber,
			String code, String password) {
		return userService.register(telephone, devicenumber, code, password);
	}

	/**
	 * 用户端点击用户头像显示用户详情信息
	 * @param devicenumber
	 * @param orderid
	 * @return
	 * @author maxmin
	 * @date 2015年9月1日下午4:40:15
	 */
	@RequestMapping(value = "detail")
	@ResponseBody
	public ReturnJsonBean detail(String devicenumber,int orderid){
		ReturnJsonBean rb = new ReturnJsonBean();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("evaluates", userService.getEvaluates(orderid));
			map.put("user", userService.getUserByOrderId(orderid));
			rb.setCode(ReturnJsonBean.SUCCESS);
			rb.setMessage(ReturnJsonBean.SUCCESS_STR);
			rb.setDatas(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rb;
	}
}
